﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using System.Linq.Expressions;

namespace BitPortal.Business
{
    public static partial class GalleryBLL
    {
        public static int Insert(this Gallery gallery)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            //Add Static
            Statistic statistic = new Statistic() { DatetimeLastModified = DateTime.Now };
            bitEnt.Statistics.AddObject(statistic);
            //Add Discusstion
            DiscussionGroup discussionGroup = new DiscussionGroup() { DatetimeCreated = DateTime.Now, LanguageCode = gallery.LanguageCode, DiscussionCount = 0 };
            bitEnt.DiscussionGroups.AddObject(discussionGroup);
            //Add gallery
            gallery.Statistic = statistic;
            gallery.DiscussionGroup = discussionGroup;
            bitEnt.Galleries.AddObject(gallery);
            return bitEnt.SaveChanges();
        }

        public static IQueryable<Gallery> GetAll()
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = from ent in bitEnt.Galleries
                       orderby ent.SortOrder descending, ent.GalleryID descending
                       select ent;
            return list;
        }

        public static Gallery GetByID(long galleryID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.Galleries.SingleOrDefault(ent => ent.GalleryID == galleryID);
        }

        public static IQueryable<Gallery> GetByCondition(string languageCode = null, string name = null, bool? isEnabled = null, int start = -1, int limit = -1, string sort = null, string dir = null)
        {
            var list = GetAll().Where(ent => (languageCode == null || ent.LanguageCode.Equals(languageCode)) &&
                    (name == null || ent.Name.Contains(name)) &&
                    (isEnabled == null || ent.IsEnabled == isEnabled)
                );

            //Start Sorting
            if (sort != null)
            {
                var param = Expression.Parameter(typeof(Gallery), "ent");
                var sortExpression = Expression.Lambda<Func<Gallery, object>>(Expression.Property(param, sort), param);
                if (dir == "DESC")
                {
                    list = list.OrderByDescending(sortExpression);
                }
                else
                {
                    list = list.OrderBy(sortExpression);
                }
            }
            //Start Paging
            if (start >= 0 && limit > 0)
            {
                list = list.Skip(start).Take(limit);
            }
            return list;
        }

        public static int Delete(int galleryID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Gallery gallery = bitEnt.Galleries.SingleOrDefault(a => a.GalleryID == galleryID);
            if (gallery != null)
            {
                Statistic statistic = gallery.Statistic;
                DiscussionGroup discussionGroup = gallery.DiscussionGroup;
                bitEnt.Galleries.DeleteObject(gallery);
                //Delete Statistic
                if (statistic != null)
                {
                    bitEnt.Statistics.DeleteObject(statistic);
                }
                //Delete DiscussionGroup
                if (discussionGroup != null)
                {
                    bitEnt.DiscussionGroups.DeleteObject(discussionGroup);
                }
                //Delete MediaInGallery - Cascade Ok
                //foreach (MediasInGallery mg in gallery.MediasInGalleries)
                //{
                //    bitEnt.MediasInGalleries.DeleteObject(mg);
                //}
                return bitEnt.SaveChanges();
            }
            return 0;

        }

        public static int AddMedia(int galleryID, long mediaID, long sortOrder = 0, string title = null, string description = null)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            if (bitEnt.MediasInGalleries.Count(ent => ent.GalleryID == galleryID && ent.MediaID == mediaID) > 0)
            {
                throw new Exception("Media đã được thêm vào gallery!");
            }
            MediasInGallery mg = new MediasInGallery()
            {
                MediaID = mediaID,
                GalleryID = galleryID,
                SortOrder = sortOrder,
                Title = title,
                Description = description
            };
            bitEnt.MediasInGalleries.AddObject(mg);
            return bitEnt.SaveChanges();
        }

        public static int DeleteMedia(int galleryID, long mediaID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            MediasInGallery mg = bitEnt.MediasInGalleries.SingleOrDefault(ent => (ent.GalleryID == galleryID && ent.MediaID == mediaID));
            if (mg != null)
            {
                bitEnt.MediasInGalleries.DeleteObject(mg);
            }
            return bitEnt.SaveChanges();
        }

        public static IQueryable<Media> GetMedia(this Gallery gallery, string type = null, bool? isEnabled = null)
        {
            return MediaBLL.GetByGallery(gallery.GalleryID, type, isEnabled);
        }
    }
}